local sk__zhenge = fk.CreateSkill {

  name = "sk__zhenge",

  tags = { Skill.Compulsory, },

}

local SkUtil = require "packages/water_jilve/utility/sk_util"


sk__zhenge:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__zhenge.name) then
      return not (data.extra_data and data.extra_data.sk__zhenge)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:getNCards(1, "bottom")
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, sk__zhenge.name, nil, false, player,
      "@@sk__zhenge-inhand")
  end,
})
sk__zhenge:addEffect(fk.TargetSpecified, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__zhenge.name) then
      return data.extra_data and data.extra_data.sk__zhenge and #data.tos == 1 and
          not data.to.dead and data.card.color ~= Card.NoColor
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    local color = data.card:getColorString()
    if room:askForSkillInvoke(player, sk__zhenge.name, nil, "#sk__zhenge_" .. color .. "-invoke::" .. to.id) then
      local info
      if color == "black" then
        info = table.random({ 1, 2, 4, 26, 27, 28, 31, 34, 35, 40, 41, 42 })
      elseif color == "red" then
        info = table.random({ 3, 5, 6, 7, 8, 9, 21, 22, 23, 24, 25, 30, 32, 33, 37, 38, 39 })
      end
      SkUtil.DoTiangongEffect(player, to, info, sk__zhenge.name)
    end
  end,
})
sk__zhenge:addEffect(fk.PreCardUse, {
  can_refresh = function(self, event, target, player, data)
    return player == target and player:hasSkill(sk__zhenge.name) and data.card:getMark("@@sk__zhenge-inhand") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.sk__zhenge = true
  end,
})

sk__zhenge:addEffect("maxcards", {
  name = "#sk__zhenge_maxcards",
  exclude_from = function(self, player, card)
    return card and card:getMark("@@sk__zhenge-inhand") > 0
  end,
})

sk__zhenge:addEffect("targetmod", {
  name = "#sk__zhenge_targetmod",
  bypass_times = function(self, player, skill, scope, card)
    return card and card:getMark("@@sk__zhenge-inhand") > 0
  end,
})

return sk__zhenge
